package com.igrowing.example.provider;

import com.igrowing.easyRpc.RpcApplication;
import com.igrowing.easyRpc.bootstrap.ProviderBootstrap;
import com.igrowing.easyRpc.config.RegistryConfig;
import com.igrowing.easyRpc.config.RpcConfig;
import com.igrowing.easyRpc.model.ServiceMetaInfo;
import com.igrowing.easyRpc.model.ServiceRegisterInfo;
import com.igrowing.easyRpc.protocol.ProtocolKeys;
import com.igrowing.easyRpc.registry.LocalRegistry;
import com.igrowing.easyRpc.registry.Registry;
import com.igrowing.easyRpc.registry.RegistryFactory;
import com.igrowing.easyRpc.server.http.VertxHttpServer;
import com.igrowing.easyRpc.server.tcp.VertxTcpServer;
import com.igrowing.example.common.service.UserService;

import java.util.ArrayList;
import java.util.List;

public class ProviderExample {

    public static void main(String[] args) {
        // 要注册的服务
        List<ServiceRegisterInfo<?>> serviceRegisterInfoList = new ArrayList<>();
        ServiceRegisterInfo<UserService> serviceRegisterInfo = new ServiceRegisterInfo<>(UserService.class.getName(), UserServiceImpl.class);
        serviceRegisterInfoList.add(serviceRegisterInfo);

        // 服务提供者初始化
        ProviderBootstrap.init(serviceRegisterInfoList);
    }

//    public static void main(String[] args) {
//        // RPC框架初始化
//        RpcApplication.init();
//        //注册服务
//        String serviceName = UserService.class.getName();
//        LocalRegistry.registry(serviceName,UserServiceImpl.class);
//        // 注册服务到注册中心
//        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
//        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
//        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
//        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
//        serviceMetaInfo.setServiceName(serviceName);
//        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
//        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
//        try {
//            registry.register(serviceMetaInfo);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//
//        if (ProtocolKeys.HTTP_PROTOCOL.equals(rpcConfig.getProtocol()))
//        {
//            // 启动Web服务
//            VertxHttpServer httpServer = new VertxHttpServer();
//            httpServer.doStart(rpcConfig.getServerPort());
//        }
//
//        else {
//            // 启动TCP服务
//            VertxTcpServer vertxTcpServer = new VertxTcpServer();
//            vertxTcpServer.doStart(rpcConfig.getServerPort());
//        }
//    }

}
